package com.example.student.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.student.R;
import com.example.student.adapter.SubjectAdapter;
import com.example.student.bean.SubjectBean;
import com.example.student.bean.TaskBean;
import com.example.student.fragment.DoTaskFragment;

import java.io.Serializable;
import java.lang.ref.PhantomReference;
import java.util.ArrayList;

public class AddTaskActivity extends AppCompatActivity implements View.OnClickListener {

    private String TAG = "AddTaskActivity";
    private TextView tv_add_subject, tv_task_type, tv_sure,tv_back;
    private EditText et_task_content;
    private CheckBox cb_task_finish, cb_task_correct;


    //    protected View mPopupView;
    protected PopupWindow popupWindow;
    private SubjectAdapter adapter;
    private ArrayList subList = new ArrayList<SubjectBean>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_task);

        initView();
        initData();
    }

    private void initView() {

        tv_back = findViewById(R.id.tv_back);

        tv_add_subject = findViewById(R.id.tv_add_subject);
        tv_task_type = findViewById(R.id.tv_task_type);

        et_task_content = findViewById(R.id.et_task_content);

        cb_task_finish = findViewById(R.id.cb_task_finish);
        cb_task_correct = findViewById(R.id.cb_task_correct);
        tv_sure = findViewById(R.id.tv_sure);

        tv_sure.setOnClickListener(this);
        tv_add_subject.setOnClickListener(this);
        tv_task_type.setOnClickListener(this);
        tv_back.setOnClickListener(this);

        cb_task_finish.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Log.e(TAG, "///cb_task_finish:" + isChecked);

            }
        });
        cb_task_correct.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Log.e(TAG, "///cb_task_correct:" + isChecked);

            }
        });


    }

    private void initData() {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if(bundle!=null){
            TaskBean doTask = (TaskBean) bundle.getSerializable("doTask");
            Log.e(TAG,"//.."+doTask.getArithmetic());
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_sure:

                break;

            case R.id.tv_add_subject:

                SubjectBean sb = new SubjectBean();
                sb.setName("语文");
                subList.clear();
                subList.add(sb);
                subList.add(new SubjectBean("英语"));
                subList.add(new SubjectBean("数学"));
                subList.add(new SubjectBean("化学"));
                subList.add(new SubjectBean("物理"));
                showPopWin(tv_add_subject,"1");
                break;

            case R.id.tv_task_type:

                subList.clear();
                subList.add(new SubjectBean("练习册"));
                subList.add(new SubjectBean("课后习题"));
                subList.add(new SubjectBean("阅读"));
                subList.add(new SubjectBean("父母出题"));
                showPopWin(tv_task_type,"2");

                break;

            case R.id.tv_back:
                finish();
                break;
        }

    }


    public void showPopWin(View view,String flag) {

        View popview = LayoutInflater.from(this).inflate(R.layout.popwin_list_view, null);

        ListView listView = popview.findViewById(R.id.listview_subject);

        adapter = new SubjectAdapter(this, subList);
        listView.setAdapter(adapter);
//        if (adapter == null) {
//        } else {
//            adapter.notifyDataSetChanged();
//        }

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Log.e(TAG, "////expressId:" + position);
                SubjectBean sub = (SubjectBean) subList.get(position);
                String name = sub.getName();
                if(flag=="1"){
                    tv_add_subject.setText(name);
                }else{
                    tv_task_type.setText(name);
                }
                popupWindow.dismiss();
            }
        });

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);

        int winWidth = (int) (metrics.widthPixels * 0.75);
        backgroundAlpha(0.5f);

        popupWindow = new PopupWindow(popview, winWidth, ViewGroup.LayoutParams.WRAP_CONTENT);

        ColorDrawable dw = new ColorDrawable(0x00);
        popupWindow.setBackgroundDrawable(dw);

        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);

        popupWindow.showAsDropDown(view, Gravity.CENTER, 0, 20);
        popupWindow.setOnDismissListener(new PopupDismissListener());

    }


    class PopupDismissListener implements PopupWindow.OnDismissListener {
        @Override
        public void onDismiss() {
            backgroundAlpha(1f);
        }
    }

    /**
     * 设置屏幕的透明度
     */
    protected void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        getWindow().setAttributes(lp);
    }
}